/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.ServiceBus;
import han.bifang.gwt.client.common.entity.ServiceCatalog;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.DomainContentConstants;
import han.bifang.gwt.client.domain.DomainDetail;
import han.bifang.gwt.client.domain.DomainMessages;

import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

/**
 * 
 * @Author Kent
 */
public class DomainDetailContent extends Content {
	private DomainMessages cons;
	private BifangGWTServiceAsync service;
	private String domainId = null;

	private DomainDetail dd = null;
	private GwtContext context;

	private VerticalPanel panel = null, p1 = null, p2 = null, p3 = null;;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			dd = (DomainDetail) res.getResponse();
			// Window.alert("" + dd.getBuses().size() + " " + dd.getCatalog());

			HTML h = new HTML(cons.domainBasic());
			p1.add(h);
			// show domain attributes
			Grid table = new Grid(2, 2);
			table.setCellPadding(1);
			table.setCellSpacing(1);
			table.setBorderWidth(1);

			ColumnFormatter cf = table.getColumnFormatter();
			cf.setWidth(0, "15%");
			cf.setWidth(1, "45%");

			table.setHTML(0, 0, cons.name());
			table.setHTML(0, 1, cons.desc());

			RowFormatter rf = table.getRowFormatter();
			rf.setStyleName(0, "tableheader");

			table.setHTML(1, 0, dd.getDomain().getName());
			table.setHTML(1, 1, dd.getDomain().getDescription());
			p1.add(table);
			HorizontalPanel h1 = new HorizontalPanel();
			// h1.setSpacing(15);
			Button b1 = new Button(cons.modify()), b2 = new Button(cons
					.remove());
			h1.add(b1);
			h1.add(b2);
			b1.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					HashMap props = new HashMap();
					props.put("domain", dd.getDomain());
					listener.onContentChanged(
							DomainContentConstants.DOMAIN_DETAIL,
							DomainContentConstants.MODIFY_DOMAIN, props);

				}

			});

			b2.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					HashMap props = new HashMap();
					props.put("domain", dd.getDomain());
					listener.onContentChanged(
							DomainContentConstants.DOMAIN_DETAIL,
							DomainContentConstants.REMOVE_DOMAIN, props);

				}

			});
			if (PrivilegChecker.hasPrivilege(context, null))
				p1.add(h1);

			// show catalog attrs
			HTML catl = new HTML(cons.catalogList());
			FlexTable table1 = new FlexTable();
			table1.setCellPadding(1);
			table1.setCellSpacing(1);
			table1.setBorderWidth(1);

			cf = table1.getColumnFormatter();
			cf.setWidth(0, "15%");
			cf.setWidth(1, "15%");
			cf.setWidth(1, "45%");

			table1.setHTML(0, 0, cons.name());
			table1.setHTML(0, 1, cons.code());
			table1.setHTML(0, 2, cons.desc());

			rf = table1.getRowFormatter();
			rf.setStyleName(0, "tableheader");

			for (int i = 0, j = dd.getCatalog().size(); i < j; i++) {
				ServiceCatalog sc = (ServiceCatalog) dd.getCatalog().get(i);
				int row = table1.getRowCount();
				table1.setHTML(row, 0, sc.getName());
				table1.setHTML(row, 1, sc.getCode());
				table1.setHTML(row, 2, sc.getDescription());
			}

			p2.add(catl);
			p2.add(table1);
			if (dd.getCatalog().size() < 1) {

				Button b3 = new Button(cons.newEntity());
				b3.addClickListener(new ClickListener() {
					public void onClick(Widget sender) {
						HashMap props = new HashMap();
						props.put("domainId", domainId);
						listener.onContentChanged(
								DomainContentConstants.DOMAIN_DETAIL,
								DomainContentConstants.CREATE_CAT1, props);

					}

				});
				if (PrivilegChecker.hasPrivilege(context, null))
					p2.add(b3);

			}

			// show catalog attrs
			HTML busl = new HTML(cons.busList());
			FlexTable table2 = new FlexTable();
			table2.setCellPadding(1);
			table2.setCellSpacing(1);
			table2.setBorderWidth(1);
			cf = table2.getColumnFormatter();
			cf.setWidth(0, "15%");
			cf.setWidth(1, "15%");
			cf.setWidth(1, "45%");

			table2.setHTML(0, 0, cons.name());
			table2.setHTML(0, 1, cons.code());
			table2.setHTML(0, 2, cons.desc());

			rf = table2.getRowFormatter();
			rf.setStyleName(0, "tableheader");

			for (int i = 0, j = dd.getBuses().size(); i < j; i++) {
				ServiceBus sc = (ServiceBus) dd.getBuses().get(i);
				table2.setHTML(i + 1, 0, sc.getName());
				table2.setHTML(i + 1, 1, sc.getCode());
				table2.setHTML(i + 1, 2, sc.getDescription());
			}

			p3.add(busl);
			p3.add(table2);
			Button b4 = new Button(cons.newEntity());
			b4.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					HashMap props = new HashMap();
					props.put("domainId", domainId);
					listener.onContentChanged(
							DomainContentConstants.DOMAIN_DETAIL,
							DomainContentConstants.CREATE_BUS1, props);

				}

			});
			if (PrivilegChecker.hasPrivilege(context, null))
				p3.add(b4);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}

	};

	public DomainDetailContent(DomainMessages cons,
			BifangGWTServiceAsync service, String domainId, GwtContext context) {
		this.cons = cons;
		this.service = service;
		this.domainId = domainId;
		this.context = context;

		panel = new VerticalPanel();
		panel.setSpacing(25);
		panel.setWidth("100%");

		initWidget(panel);

	}

	public void onLoad() {

		p1 = new VerticalPanel();
		p2 = new VerticalPanel();
		p3 = new VerticalPanel();
		p1.setSpacing(0);
		p2.setSpacing(0);
		p3.setSpacing(0);
		p1.setWidth("100%");
		p2.setWidth("100%");
		p3.setWidth("100%");
		panel.add(p1);
		panel.add(p2);
		panel.add(p3);
		RequestObject req = new RequestObject();
		req
				.setAction("han.bifang.gwt.server.action.domain.GetDomainDetailAction");
		req.setRequest(domainId);
		service.call(req, cb);

	}

}
